package cy;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class JSONTest02 {
    @Test
    public void test01(){
        student student = new student(20131210,"无忌","男",25);
        System.out.println(JSON.toJSONString(student));
    }
    @Test
    public void test02(){
        String jsonStr="{\"age\":25,\"gender\":\"男\",\"sname\":\"无忌\",\"sno\":20131210}";
        student jsonObject = JSON.parseObject(jsonStr,student.class);
        System.out.println(jsonObject);
    }
    @Test
    public void test03(){
        List<student> students = Arrays.asList(
                new student(20131210, "无忌", "男", 25),
                new student(20131217, "敏敏", "女", 20)
        );
        System.out.println(JSON.toJSONString(students));
        System.out.println(JSON.toJSONString(students, JSONWriter.Feature.PrettyFormat));
    }
    @Test
    public void test04(){
        String jsonStr="[{\"sno\":20131210,\"sname\":\"无忌\",\"gender\":\"男\",\"age\":25},{\"sno\":20131210,\"sname\":\"敏敏\",\"gender\":\"女\",\"age\":20}]";
        JSON.parseArray(jsonStr,student.class).forEach(System.out::println);
    }
    @Test
    public void test05(){
        List<student> students = Arrays.asList(
                new student(20131210, "无忌", "男", 25),
                new student(20131217, "敏敏", "女", 20)
        );
        Map<Integer, student> map = students.stream().collect(Collectors.toMap(student -> student.getSno(), student -> student));
        System.out.println(JSON.toJSONString(map));
        System.out.println(JSON.toJSONString(map, JSONWriter.Feature.PrettyFormat));
    }
}
